package gateway.mgt

import gateway.mgt.api.ApiDefinition
import gateway.mgt.api.ApiGroup
import grails.testing.mixin.integration.Integration
import grails.transaction.Rollback
import spock.lang.Specification

@Rollback
@Integration(applicationClass=mgtcore.Application)
class ApiGroupIntegrationSpec extends Specification {

    static ApiGroup apiGroupInstance = new ApiGroup(name: "default")

    static final ApiDefinitionPropertiesSet = [
            [
                    apiGroup: apiGroupInstance, name: "demo1", description: "demo api 1",
                    request : [path: "/Service-Set-1/sampleApi1"], backend: [host: "lb://Service-Set-1", path: "/sampleApi1"]
            ],
            [
                    apiGroup: apiGroupInstance, name: "demo2", description: "demo api 2",
                    request : [path: "/Service-Set-2/sampleApi2"], backend: [host: "lb://Service-Set-2", path: "/sampleApi2"]
            ]
    ]

    def setup() {
        apiGroupInstance.save(flush: true)
        ApiDefinitionPropertiesSet.each { new ApiDefinition(it).save(flush: true) }
    }

    def cleanup() {
    }

    void "test method 'publish()'"() {
        when:
        def result = apiGroupInstance.publish()

        then:
        result.containsKey("commitId")
        result.containsKey("remoteUpdates")
        result.remoteUpdates.size() == 1
    }
}
